package com.yupi.springbootinit.controller;

import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Description
 * @Author minify
 * @Date 2023/12/16 16:10
 * @Project mybi-backend
 */
@RestController
@RequestMapping("/task")
@Profile("test")
public class TaskController {

    @Resource
    private ThreadPoolExecutor threadPoolExecutor;

    @GetMapping("/test")
    public void test(String name) {
        CompletableFuture.runAsync(() -> {
            System.out.println("异步任务开始执行..."+name+"执行人"+Thread.currentThread().getName());
            try {
                Thread.sleep(60000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, threadPoolExecutor);
    }

    /**
     * 查看线程池状态
     */
    @GetMapping("/status")
    public String status() {
        return "当前活跃线程数：" + threadPoolExecutor.getActiveCount()
                + "，当前线程池大小：" + threadPoolExecutor.getPoolSize()
                + "，当前排队线程数：" + threadPoolExecutor.getQueue().size();
    }
}
